from apscheduler.triggers.date import DateTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger
from apscheduler.schedulers.background import BackgroundScheduler
import random
from .task import *

class InherentAddTask:
    def __init__(self,scheduler_obj:BackgroundScheduler):
        self.scheduler = scheduler_obj
        # 执行对象中所有以task开头的方法
        name_list = [name for name in dir(self) if callable(getattr(self, name)) and name.startswith("task")]
        for name in name_list:
            func = getattr(self,name)
            func()


    def task_send_to_big_data(self):
        self.scheduler.add_job(
            send_to_big_data,
            args=(10,20),
            trigger=IntervalTrigger(minutes=1),
            id='send_to_big_data',
            replace_existing=True,
            coalesce=True,
            name='该定时，用于将数据推送到远端大数据系统'
        )
    def task_delete_apscheduler_history(self):
        # 每天凌晨，清除历史的记录
        self.scheduler.add_job(
            delete_apscheduler_history,
            trigger=CronTrigger(hour=0, minute=0, second=1),
            id='delete_apscheduler_history',
            replace_existing=True,
            coalesce=True,
            name='该定时器，是将历史定时任务的执行记录进行清除'
        )

    def task_crontab_task(self):
        # 每隔20分钟清除垃圾记录
        self.scheduler.add_job(
            crontab_task,
            trigger=IntervalTrigger(minutes=20),  # 每天晚上零点1秒执行
            id='crontab_task',
            replace_existing=True,
            coalesce=True,
            name='该定时任务，用于清除xxx表中的垃圾记录'
        )

    def task_date_task(self):
        self.scheduler.add_job(
            date_task,
            trigger=DateTrigger(run_date='2024-12-03 10:11:30'),
            id='date_task',
            replace_existing=True,
            coalesce=True,
            name='该定时任务，在指定时间，进行系统初始化任务',
        )